/**
 * 生成CheckBox 和 Radio 组
 * @author zhaojh
 */
function CheckBoxOrRadioGroupObj(o){
	this.editor = o;
}

CheckBoxOrRadioGroupObj.prototype.onReady = function(){
	var $groupDiv = jQuery(this.editor);
	var label = $groupDiv.attr("label");

	if(label && jQuery.trim(label) != ""){
		$groupDiv.prepend(label+"<span>&nbsp;</span><br>");
	}else{
		$groupDiv.prepend("<span>&nbsp;</span><br>");
	}
	
	//取得要操作的CheckBox、Radio对象
	var $chkobj;
	if($groupDiv.attr("type") == "checkbox"){
        $chkobj = $groupDiv.children("input[type=checkbox]");  
        $groupDiv.children("input[type=checkbox]").remove();        
    }else{
        $chkobj = $groupDiv.children("input[type=radio]");
        $groupDiv.children("input[type=radio]").remove();
    }
	
    //给每个具体checkbox和radio赋validator属性
    $chkobj.each(function(){
        jQuery(this).attr("validator",$groupDiv.attr("validator"));
    });
    
	var row = $groupDiv.attr("row");
	var col = $groupDiv.attr("col");
	
	//判断是否设置的按几行、几列布局，如果没有则不做处理都显示在一行上
	if(row && col){
		//当行为动态时根据列自动算行数
		if(row == "*" && col != "*"){
			row = ($chkobj.length % col == 0 ? $chkobj.length / col : $chkobj.length / col + 1);
		}
		//当列是动态时，根据行自动算列
		if(row != "*" && col == "*"){
			col = ($chkobj.length % row == 0 ? $chkobj.length / row : $chkobj.length / row + 1);
		}
		this.creatLayout($groupDiv, $chkobj, row, col);
	}else{
		for(var i=0; i<$chkobj.length ; i++){
            $groupDiv.append(jQuery($chkobj[i])).append(jQuery($chkobj[i]).attr("label")).append("&nbsp;&nbsp;&nbsp;");
        }
	}
}

CheckBoxOrRadioGroupObj.prototype.creatLayout = function($container, $chkobj, row, col){
	var $groupDiv = $container;
	var $table = jQuery("<table>").appendTo($groupDiv);
	
	//先按照行列生成布局表格
	for(var i=0; i<row; i++){
		//生成行
		var $tr = jQuery("<tr>").appendTo($table);
		//生成列
		for(var j=0; j<col; j++){
			var $td = jQuery("<td>").appendTo($tr);
		}
	}
	
	//往布局表格中插入CheckBox、Radio

	var $tr_ = $table.children("tbody").children("tr");
	for(var row=0,i=0;row<$tr_.length && i<$chkobj.length;row++,i++){
		var $td = jQuery($tr_[row]).children("td");
		for(var j=0; j<$td.length ; j++){
            jQuery($td[j]).append(jQuery($chkobj[i])).append(jQuery($chkobj[i]).attr("label")).append("&nbsp;&nbsp;&nbsp;");
            i++;
        }
		//因为前面多加一次，所以再减1
		i--;
	}
}

/**
 * 取得被选中的CheckBox或Radio的value值
 */
CheckBoxOrRadioGroupObj.prototype.getSelected = function(){
	var $o = jQuery(this.editor).find("input[type=checkbox][type=radio][checked]");
	var type = jQuery(this.editor).attr("type");
	var $o;
	var result= new Array();
	if(jQuery.trim(type) == "checkbox"){
		$o = jQuery(this.editor).find("input[type=checkbox][checked]");
		for(var i=0; i<$o.length; i++){
			result.push(jQuery($o[i]).attr("value"));
		}
	}
	
	if(jQuery.trim(type) == "radio"){
		$o = jQuery(this.editor).find("input[type=radio][checked]");
        for(var i=0; i<$o.length; i++){
            result.push(jQuery($o[i]).attr("value"));
        }
	}
	
	if(result.length>0){
		return result.join(",");
	}else{
		return "";
	}
}

/**
 * 全选
 */
CheckBoxOrRadioGroupObj.prototype.checkAll = function(){
	var type = jQuery(this.editor).attr("type");
	
	if(jQuery.trim(type) == "checkbox"){
		jQuery(this.editor).find("input[type=checkbox]").attr("checked","true");
	}else{
		jQuery(this.editor).find("input[type=radio]").attr("checked","true");
	}
}

/**
 * 全取消
 */
CheckBoxOrRadioGroupObj.prototype.unCheck = function(){
    var type = jQuery(this.editor).attr("type");
    
    if(jQuery.trim(type) == "checkbox"){
        jQuery(this.editor).find("input[type=checkbox]").removeAttr("checked");
    }else{
        jQuery(this.editor).find("input[type=radio]").removeAttr("checked");
    }    
}
